﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;

using DflyingForum.Core.Data.Domain.Entities;
using DflyingForum.Core.Data.NHHelper;
using DflyingForum.Core.Data.Domain.ValueObjects;
using DflyingForum.Core.Framework.DflyingPortal;
using DflyingForum.Core.Framework;

namespace Core.Web.Code.UI.Controls.ChildControls
{
    public partial class PostList : BaseControl
    {
        protected Repeater rptPostContent;
        protected Repeater rptTopPosts;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GetDadaBind();
            }
        }

        private void GetDadaBind()
        {
            if (TopicId > 0)
                _query.Add(new CriteriaParameter("Topic.ID", TopicId, CriteriaOperator.Eq));
            if (ModuleId > 0)
                _query.Add(new CriteriaParameter("Module.ID", ModuleId, CriteriaOperator.Eq));//关联查询
            if (StateId >= 0)
            {
                if (StateId == 3)//自荐精华
                    _query.Add(new CriteriaParameter("Flag", (int)EnumPostState.BeingExcellent, CriteriaOperator.Eq, "PostStates"));
                if (StateId == 2)//热门
                {
                    _query.Add(new CriteriaParameter("ReplyCount", 40, CriteriaOperator.Ge));
                    _query.Add(new CriteriaParameter("Hits", 100, CriteriaOperator.Ge));
                    _query.Add(new CriteriaParameter("HotPercent", 0.014, CriteriaOperator.Ge));
                }
                if (StateId == 1)//版主推荐
                {
                    _query.Add(new CriteriaParameter("Flag", (int)EnumPostState.Recommendation, CriteriaOperator.Eq, "PostStates"));
                }
                if (StateId == 0)//精华
                    _query.Add(new CriteriaParameter("Flag", (int)EnumPostState.Excellent, CriteriaOperator.Eq, "PostStates"));
            }
            //    _query.Add(new CriteriaParameter("StateID", StateId, CriteriaOperator.Eq));
            _query.Add(new CriteriaParameter("IsTop", false, CriteriaOperator.Eq));

            _query.Add(new CriteriaParameter("CreateTime", SelectTime, CriteriaOperator.Ge));
            _query.Add(new CriteriaParameter(SortBy, CriteriaOperator.OrderByDesc));//排序

            IList<Posts> result = _bllPost.FindByPage(PageIndex, PageSize, out RecordCount, _query);

            rptPostContent.DataSource = result;
            rptPostContent.DataBind();

            if (PageIndex == 1)
            {
                rptTopPosts.Visible = true;
                rptTopPosts.DataSource = _bllPost.GetTopPost();
                rptTopPosts.DataBind();
            }
        }

        protected void rptTopPosts_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Posts itemPost = e.Item.DataItem as Posts;
                if (itemPost != null)
                {
                    BindImg(itemPost, e);
                }
            }
        }

        protected void rptPostContent_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Posts itemPost = e.Item.DataItem as Posts;
                if (itemPost != null)
                {
                    BindImg(itemPost, e);
                }
            }
        }

        //绑定各种状态的图片和最后发表人及帖子
        private void BindImg(Posts itemPost, RepeaterItemEventArgs e)
        {
            #region 绑定图片
            Regex reg = new Regex(@"<img[\s\S]+>", RegexOptions.IgnoreCase);
            if (reg.Match(itemPost.Body).Success)//喊有图片附件
            {
                Image Img_Attach = e.Item.FindControl("Img_Attach") as Image;
                if (Img_Attach != null)
                {
                    Img_Attach.Visible = true;
                    Img_Attach.ImageUrl = "~/Skins/MyTheme/images/post_img.gif";
                }
            }
            if (_bllPost.HowToGetHotPost(itemPost.ID))//热帖
            {
                Image Img_Hot = e.Item.FindControl("Img_Hot") as Image;
                if (Img_Hot != null)
                {
                    Img_Hot.Visible = true;
                    Img_Hot.ImageUrl = "~/Skins/MyTheme/images/post_hot.gif";
                }
            }
            if (_bllPost.IsExcellent(itemPost))
            {
                Image Img_Digest = e.Item.FindControl("Img_Digest") as Image;
                if (Img_Digest != null)
                {
                    Img_Digest.Visible = true;
                    Img_Digest.ImageUrl = "~/Skins/MyTheme/images/post_digest.gif";
                }
            }
            if (_bllPost.IsGiveAmount(itemPost))//帖子被加分，算法待定
            {
                Image Img_Agree = e.Item.FindControl("Img_Agree") as Image;
                if (Img_Agree != null)
                {
                    Img_Agree.Visible = true;
                    Img_Agree.ImageUrl = "~/Skins/MyTheme/images/post_score.gif";
                }
            }
            #endregion

            Label lblReplyTime = e.Item.FindControl("lblReplyTime") as Label;
            Label lblReplyUser = e.Item.FindControl("lblReplyUser") as Label;

            User u = _bllUser.FindBy(itemPost.LastReplyUserID);
            if (u != null)
                lblReplyUser.Text = u.NickName;
            else
                lblReplyUser.Text = itemPost.PubUser.NickName;

            Reply lastReply = _bllReply.FindBy(itemPost.LastReplyID);
            if (lastReply != null)
                lblReplyTime.Text = lastReply.CreateTime.ToString("yyyy-MM-dd hh:mm");
            else
                lblReplyTime.Text = itemPost.CreateTime.ToString("yyyy-MM-dd hh:mm");
        }

        //获取普通主题(非置顶)标题前显示的图片
        protected string GetImg(object oId)
        {
            int postId = Convert.ToInt32(oId);
            Posts p = _bllPost.FindBy(postId);
            IList<PostState> pss = p.PostStates;
            foreach (var item in pss)
            {
                if (EnumPostState.Notice == (EnumPostState)item.Flag)//全局通告 优先级0
                    return "Skins/MyTheme/images/post_notice.gif";
                if (EnumPostState.GlobalTop == ((EnumPostState)item.Flag))//全局置顶 优先级1
                    return "Skins/MyTheme/images/post_top_3.gif";
                if (EnumPostState.TopicTop == ((EnumPostState)item.Flag))//分类置顶 优先级2
                    return "Skins/MyTheme/images/post_top_2.gif";//版块置顶
                if (EnumPostState.ModuleTop == ((EnumPostState)item.Flag))// 优先级3
                    return "Skins/MyTheme/images/post_top_1.gif";
                if (EnumPostState.Lock == (EnumPostState)item.Flag)//锁定  优先级4
                    return "Skins/MyTheme/images/post_lock.gif";
                //其他 加亮  提前x
                if (EnumPostState.HighLight == (EnumPostState)item.Flag)//加亮
                    return "Skins/MyTheme/images/post_light.gif";
            }
            return "Skins/MyTheme/images/post_common.gif"; //优先级最后
        }

        #region 外部需要赋值字段
        //需要获取 分类、模块、排序{默认、发布时间、回复时间、点击次数、回复次数}、
        //筛选{（全部、今天、两天、一周、一个月、三个月）}、状态{精华、推荐、热门}
        public int TopicId { get; set; }
        public int ModuleId { get; set; }
        public string SortBy { get; set; }

        private DateTime _selectTime = DefaultValue.DefTime;
        public DateTime SelectTime//筛选时间
        {
            get { return _selectTime; }
            set { _selectTime = value; }
        }
        public int StateId { get; set; }//精华0|自荐精华3|热门2|推荐1
        #endregion
    }
}